<?php

namespace app\yperp\common;

use dragonlhp\amisbuilder\builder\FormBuilder;
use dragonlhp\amisbuilder\builder\PageBuilder;
use dragonlhp\amisbuilder\builder\TableBuilder;
use think\facade\Config;

trait AmisController
{
    protected $table = null;
    protected $form = null;
    protected $page = null;


    protected function  initialize()
    {
        parent::initialize();

        $this->table = TableBuilder::getInstance(Config::get('amis.table'));
        $this->form = FormBuilder::getInstance(Config::get('amis.form'));
        $this->page = PageBuilder::getInstance(Config::get('amis.page'));
    }
    protected function fetch_table()
    {
        $this->page->setBody($this->table->toArray());
        $fetch = $this->page->fetch();
        // $this->assign('Attribute', $fetch['vars']['attribute']);
        $this->assign('jsonpage',  json_encode($this->page->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        return $this->fetch();
    }
    protected function fetch_form()
    {
        $this->page->setBody($this->form->toArray());
        $fetch = $this->page->fetch();
        // $this->assign('Attribute', $fetch['vars']['attribute']);
        $this->assign('jsonpage',  json_encode($this->page->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        return $this->fetch();
    }
    protected $type = 'page';
    protected function fetchs($template = '', $vars = [], $config = [])
    {
        switch ($this->type) {
            case 'table':
                $this->page->setBody($this->table->toArray());
                break;
            case 'form':
                $this->page->setBody($this->form->toArray());
                break;
            case 'page':
            default:
        }
        $fetch = $this->page->fetch();
        // $this->assign('Attribute', $fetch['vars']['attribute']);
        // dump(json_encode($this->page->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));die;
        // View::assign('jsonpage',  $this->page->tojson());

        json_encode($this->page->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        $fetch = $this->page->fetch();
        // dump($fetch);
        return parent::fetch($fetch['tpl']);
    }
}
